package com.qingke.easyjava.springhibernate.service;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.qingke.easyjava.springhibernate.bean.User;

@Repository
public class HibernateManager {

    @Autowired
    protected SessionFactory sessionFactory;
    
    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }
    
    public Session getNewSession() {
        return sessionFactory.openSession();
    }

    public void flush() {
        getSession().flush();
    }
    
    public void clear() {
        getSession().clear();
    }

    public <T> T get(Class<T> c, String id) {
        Session session = getSession();
        return session.load(c, id);
    }

    public <T> List<T> getAllList(Class<T> c) {
        Session session = getSession();
        DetachedCriteria dc = DetachedCriteria.forClass(c);
        return dc.getExecutableCriteria(session).list();
    }
    
    public <T> long getTotalCount(Class<T> c) {
        return getAllList(c).size();
    }

    public <T> T getObjectByCode(Class<T> clazz, String code) {
        DetachedCriteria criteria = DetachedCriteria.forClass(clazz);
        criteria.add(Restrictions.eq("code", code));
        Criteria result = criteria.getExecutableCriteria(getSession());
        T object = (T) result.uniqueResult();
        
        return object;
    }
    
    public void save(Object bean) {
        try {
            getSession().save(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public Criteria getCriteria(Class<?> classType){
		DetachedCriteria detachedCriteria =DetachedCriteria.forClass(classType);
		Criteria criteria =detachedCriteria.getExecutableCriteria(getSession());
		return criteria;
	}
    
    public  boolean isUserExists(String username,String password){
    	Criteria criteria =getCriteria(User.class);
    	criteria.add(Restrictions.eq("username", username));
    	criteria.add(Restrictions.eq("password", password));
    	List<?> list=criteria.list();
    	if(list.size()>0){
    		return true;
    	}
    	return false;
    }
    
    public boolean isObjectExists(Class<?> classType,String username){
    	Criteria criteria =getCriteria(classType);
    	criteria.add(Restrictions.eq("username", username));
    	List<?> list =criteria.list();
    	if(list.size()>0){
    		return true;
    	}
    	return false;
    }
}
